var Menu = (function($){
	orderList_CallBack = function(data){
		if(data.code == "200"){
			$.hideLoading();
			if(data.data.length == 0){
				var list_item = $("<li style='text-align: center;'>暂无订单</li>");
				list_item.appendTo($("#order-list").find(".menu-list"));
			}
			for(var i = 0; i < data.data.length; i++){
				domLoad(data.data[i]);
			}
		}else if(data.code == "201"){
			$.hideLoading();
			$.toast("查询失败", "forbidden",function(){
				$.closePopup();
			});
		}else{
			$.hideLoading();
			$.alert("登录异常，请登录"); 
		}

		function domLoad(order){
			var list = $("#order-list").find(".menu-list");
			var list_item = $("<li></li>");
			var p1 = $("<p><img src='img/fn3.png'/></p>");
			var p2 = $("<p><img src='img/fn2.png'/></p>");
			p1.appendTo(list_item);
			p2.appendTo(list_item);

			order.startLocation = order.startLocation.length > 9 ? order.startLocation.substr(0,8)+"..." : order.startLocation;
			
			var order_start = $('<span class="order-start"></span>').text(order.startLocation);
			var order_date = $('<span class="order-date"></span>').text(order.createTime);
			var order_end = $('<span class="order-end"></span>').text(order.endLocatin);

			switch (order.status) {
				case 0: 
					order.status = "可重新叫车"; 
					list_item.on("click",function(){
						$.closePopup();
					});
					break;
				case 1: order.status = "未支付"; break;
				case 2: order.status = "未评价"; break;
				case 3: order.status = "已取消"; break;
				case 4: order.status = "已完成"; break;
				case 7: order.status = "订单待接受";
					list_item.on("click",function(){
						sessionStorage.driver = JSON.stringify(order.driver);
						window.location.href = "page_callCarLoading.html";
					});
					break;
				case 8: order.status = "订单已接受";
					list_item.on("click",function(){
						sessionStorage.driver = JSON.stringify(order.driver);
						window.location.href = "page_driverAllow.html";
					});
				 	break;
				default:
					order.status = "订单已关闭";
					break;
			}
			var order_status = $('<span class="order-status"></span>').text(order.status);
			order_start.appendTo(p1);
			order_date.appendTo(p1);
			order_end.appendTo(p2);
			order_status.appendTo(p2);
			list_item.appendTo(list);
		}
	};
	var cancelCall_CallBack = function(data){
		if (data.code == "200") {
			$.toast("已取消叫车");	
			Build.getMsg(true);
			setTimeout(function(){
				window.location.href = "index.html";
			},1000)
		}else{
			$.toast('The operation failure!',"forbidden");
		}
	};
	return{
		showSideMenu: function(){
			if(!sessionStorage.USER){
				$(".modal-wall,#login-wrap").show();
			}else{
				$("#left_menu").find(".user_number").text(JSON.parse(sessionStorage.USER).phoneNumber);

				$("#left_menu").animate(
					{"left": "0"},"slow");
				$(".modal-wall").show();
				$(".modal-wall").click(function(){
					$("#left_menu").animate(
						{"left": "-100%"},"slow");
					$(".modal-wall").hide();
				})
			}
		},
		allClose: function(){
			$(".modal-wall,.modal-wrap").hide();
		},
		autoInput: function(){
			$(".tel-code").each(function(i){
				this.onkeyup =  function(){
					if(this.value.length == 1){
						if(i != 3){
							$(".tel-code")[i+1].focus();
						}else{
							$("#code-btn").focus();
						}
					}
				}
			})
		},
		orderList: function(){
			$("#left_menu").animate(
				{"left": "-100%"},"slow");
			$(".modal-wall").hide();
			$("#order-list").popup();
			$("#order-list").find(".menu-list").html("");
			$.showLoading("正在查询订单");
			Build.request({
				type: "get",	
				data: "",
				port: "/order/getUserTop5Order"
			}, orderList_CallBack, false)
		},
		cancelCall: function(){
			$.confirm("你确定要取消叫车吗？", function(){
				$.showLoading("正在取消叫车");
				Build.getMsg(true);
				Build.request({
					type: "get",
					data: "",
					port: "/taxi/cancelOrder",
				},cancelCall_CallBack, false);
			})
		},
	}
})(jQuery);

var settingInit = (function($){
	$(".weui_btn").mousedown(function(){
		this.style.backgroundColor = "#339AFF";
	});
	var ourl = window.location.search.substr(1);
	switch (ourl) {
		case "data=account":{
			$("#account-link").show();
			$(".setting-list").eq(0).hide();
			$(".setting-logout").hide()
			$("#setting_title").text("账户与安全");
			break;
		}
		case "data=aboutApp":{
			$("#aboutApp-link").show();
			$(".setting-list,.setting-logout").hide();
			$("#setting_title").text("关于全唐通");
			break;
		}
		case "data=account&set=phone":{
			$("#aboutApp-link").hide();
			$(".setting-list,.setting-logout").hide();
			$("#account-phone").show();
			$("#setting_title").text("手机号");
			break;
		}
		case "data=account&set=pwd":{
			window.location.href = "page_changepwd.html";
			break;
		}
		default: {
			console.log('no url data!')
			break;
		}
	}
})(jQuery)
